package top.kikt.book.core.handlers

import okhttp3.HttpUrl.Companion.toHttpUrl
import top.kikt.book.core.handlers.common.BaseBookHandler
import top.kikt.book.core.handlers.common.CommonHandler
import top.kikt.book.util.urlEncode
import java.nio.charset.Charset

class Picdg : CommonHandler, BaseBookHandler() {

    override fun watermark(): Map<Regex, String> {
        return mapOf(
            Regex("&nbsp;") to " ",
            Regex("本文链接:") to "",
            Regex("感谢：.+打赏.+") to "",
        )
    }

    override val host: String
        get() = "www.picdg.com"

    override fun searchBook(keyword: String): List<String> {
        val url = "https://www.picdg.com/search.php"

        val formData = mapOf(
            "searchkey" to keyword.urlEncode()
        )

        val response = url.toHttpUrl().postForm(formData)

        if (response.code == 302) {
            return listOf(response.header("Location")!!)
        }

        return super.searchBook(keyword)
    }

    override fun forceCharset(): Charset? = null
}
